package com.b.sts.controller;

import com.b.sts.entity.UserEntity;
import com.b.sts.service.UserService;
import com.b.sts.util.ResultUtil;
import com.b.sts.vo.ResultVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/{id}")
    public ResultVO<UserEntity> getById(@PathVariable Long id) {
        return ResultUtil.success(userService.getById(id));
    }

    @GetMapping("/list")
    public ResultVO<List<UserEntity>> list() {
        return ResultUtil.success(userService.list());
    }

    @GetMapping("/page")
    public ResultVO<Page<UserEntity>> page(@RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize) {
        return ResultUtil.success(userService.page(new Page<>(pageNum, pageSize)));
    }

    @PostMapping
    public ResultVO<Boolean> add(@RequestBody UserEntity userEntity) {
        boolean isSuccess = userService.save(userEntity);
        return ResultUtil.autoDo(isSuccess);
    }

    @DeleteMapping("/{id}")
    public ResultVO<Boolean> del(@PathVariable Long id) {
        boolean isSuccess = userService.removeById(id);
        return ResultUtil.autoDo(isSuccess);
    }

    @PutMapping
    public ResultVO<Boolean> update(@RequestBody UserEntity userEntity) {
        boolean isSuccess = userService.updateById(userEntity);
        return ResultUtil.autoDo(isSuccess);
    }
}
